﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AvalonDock.Layout;
using AutomationMLMapper.ViewModels.PaneViewModels;
using AutomationMLMapper.ViewModels.SystemViewModels;
using System.Windows;

namespace AutomationMLMapper.Controls
{
    /// <summary>
    /// The Layout Initializer manages the layout of floating tool windows. 
    /// </summary>
    public class LayoutInitializer: ILayoutUpdateStrategy
    {
        /// <summary>
        /// Routine to place an anchorable of a specific type to a predefined destination pane, according to its content
        /// </summary>
        /// <param name="layout"></param>
        /// <param name="anchorableToShow"></param>
        /// <param name="destinationContainer"></param>
        /// <returns></returns>
        public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
        {
            //AD wants to add the anchorable into destinationContainer
            LayoutAnchorablePane destPane = destinationContainer as LayoutAnchorablePane;
            if (destinationContainer != null &&
                destinationContainer.FindParent<LayoutFloatingWindow>() != null)
                return false;

            if (anchorableToShow.Content is SupportedRolesViewModel)
            {
                LayoutAnchorablePane toolsPane = null;

                var vm = anchorableToShow.Content as SupportedRolesViewModel;

                anchorableToShow.IsVisibleChanged += new EventHandler(anchorableToShow_IsVisibleChanged);

                toolsPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "ToolPane");

                if (toolsPane == null)
                {
                    if (vm.CollectionOwner.TheSimulatedSystem is TargetSystemViewModel)
                        toolsPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "TargetSystemPane");
                    else if (vm.CollectionOwner.TheSimulatedSystem is SourceSystemViewModel)
                        toolsPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "SourceSystemPane");

                    if (toolsPane != null)
                    {
                        var paneGroup = toolsPane.Parent as LayoutAnchorablePaneGroup;
                        if (paneGroup != null)
                        {
                            toolsPane = new LayoutAnchorablePane(anchorableToShow);
                            toolsPane.DockWidth = new GridLength(40, GridUnitType.Star);
                            toolsPane.Name = "ToolPane";
                        }
                        paneGroup.Children.Add(toolsPane);

                        return true;
                    }
                }
            }

            else if (anchorableToShow.Content is DifferentRolesViewModel)
            {
                LayoutAnchorablePane toolsPane = null;

                var vm = anchorableToShow.Content as DifferentRolesViewModel;

                anchorableToShow.IsVisibleChanged += new EventHandler(anchorableToShow_IsVisibleChanged);
                toolsPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "ToolPane");

                if (toolsPane == null)
                {
                    toolsPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "SourceSystemPane");

                    if (toolsPane != null)
                    {
                        var paneGroup = toolsPane.Parent as LayoutAnchorablePaneGroup;
                        if (paneGroup != null)
                        {
                            toolsPane = new LayoutAnchorablePane(anchorableToShow);
                            toolsPane.DockWidth = new GridLength(40, GridUnitType.Star);
                            toolsPane.Name = "ToolPane";
                        }
                        paneGroup.Children.Add(toolsPane);

                        return true;
                    }
                }
                else if (toolsPane != null)
                {
                    toolsPane.Children.Add(anchorableToShow);
                    return true;
                }
            }

            return false;
        }

                

        /// <summary>
        ///  Visibility Changes of any Layout Item should update the Hidden Window - Property in the MainView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void anchorableToShow_IsVisibleChanged(object sender, EventArgs e)
        {
            AutomationMLMapperMain.MainView.UpdateHiddenWindowProperty();
        }


        /// <summary>
        /// not used
        /// </summary>
        /// <param name="layout"></param>
        /// <param name="anchorableShown"></param>
        public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableShown)
        {
            //if (anchorableShown.Content is PaneViewModel)
            //{
            //    if ((anchorableShown.Content as PaneViewModel).IsFloating)
            //    {
            //        (anchorableShown.Content as PaneViewModel).IsFloating = false;
            //        anchorableShown.FloatingWidth = 420;
            //        anchorableShown.FloatingHeight = 250;
            //        anchorableShown.Float();
            //    }
            //} 
        }
    }
}
